<?php

/**
 * Document actions.
 *
 * @package    DocFlow
 * @subpackage Document
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DocumentActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->documents = DocumentTable::getInstance()->findAllExistingDocumentsOrderedByCategories();
      $this->redirect('@mydocs');
  }

  public function executeSearch(sfWebRequest $request)
  {
    $this->documents = null;
    $query = $request->getPostParameter('query');

    if (!empty($query))
    {
      $parameters["name"]="false";
      $parameters["creator"]="false";
      $parameters["description"]="false";
      $parameters["category"]="false";
     
      if ($request->getParameter('name')!=null)
        $parameters["name"]="true";
      if ($request->getParameter('creator')!=null)
        $parameters["creator"]="true";
      if ($request->getParameter('description')!=null)
        $parameters["description"]="true";
      if ($request->getParameter('category')!=null)
        $parameters["category"]="true";

      $this->documents = DocumentTable::getInstance()->getForLuceneQuery($query,$parameters);
//      if ($request->isXmlHttpRequest())
      {
        if ('*' == $query || !$this->documents)
        {
          return $this->renderText('No results');
        }
        else
        {
          return $this->renderPartial('Document/list', array('documents' => $this->documents));
        }
      }
    }
  }

  public function executeInfo (sfWebRequest $request)
  {
    $this->documents = null;
    $document_id = $request->getParameter('id');
    $this->forward404Unless($document_id, sprintf('Parameter is wrong (%s).', $document_id));

    $this->document = DocumentTable::getInstance()->findDocumentById($document_id);
    $this->forward404Unless($this->document, 
        sprintf('Object document does not exist (%s).', $document_id));

    $creator_id = DocumentVersionTable::findCreatorId($document_id);		
    $documentCreatorUser = sfGuardUserTable::getInstance()->find($creator_id);
    $documentCreator = sfGuardUserProfileTable::getInstance()->findOneBy('user_id',$documentCreatorUser->getId());
    $this->forward404Unless($documentCreator, 
        sprintf('Object user does not exist (%s).', $creator_id));
    $this->documentCreatorFullName = $documentCreator->getFullName();

    $updator_id = $this->document->getAuthorId();
    $documentUpdator = sfGuardUserProfileTable::getInstance()->findOneBy('user_id',$updator_id);
    $this->forward404Unless($documentUpdator,
        sprintf('Object user does not exist (%s).', $updator_id));
    $this->documentUpdatorFullName = $documentUpdator->getFullName();

    $this->getUser()->addDocumentToHistory($document_id);

    $this->show_document=true;
    $user_id = $this->getUser()->getId();
    if ($creator_id != $user_id)
      $this->show_document=false;
  }

  public function executeMydocs(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::GET));
    
    $this->user_name = $this->getUser()->getUsername();
    
    $user_id = $this->getUser()->getId();
    $this->documents = DocumentVersionTable::getInstance()->
    	findAllExistingDocumentsModifiedByUserOrderedByCategories($user_id);
  }

  
  public function executeVersions(sfWebRequest $request)
  {

    $document_id = $request->getParameter('id');
    $this->forward404Unless($document_id, sprintf('Parameter is wrong (%s).', $document_id));

    $this->documents = DocumentVersionTable::getInstance()->getDocumentVersionHistory($document_id);

    $this->document = DocumentTable::getInstance()->find($document_id);
    $this->forward404Unless($this->document, sprintf('Object document does not exist (%s).', $document_id));

    $this->getUser()->addDocumentToHistory($document_id);

    $this->show_file=true;
    $user_id = $this->getUser()->getId();
    $creator_id = DocumentVersionTable::findCreatorId($document_id);
    if ($creator_id != $user_id)
      $this->show_file=false;
  }
  
  public function executeNew(sfWebRequest $request)
  {
    $document = new Document();
    $document->setAuthorId($this->getUser()->getId());
  	$this->form = new DocumentForm($document);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $documentPost = $request->getPostParameter('document');
    $this->forward404Unless($documentPost, sprintf('Object document does not exist.'));

    $document = new Document($documentPost);
    $document->setAuthorId($this->getUser()->getId());

    $this->form = new DocumentForm($document);
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }
  
  public function executeEdit(sfWebRequest $request)
  {
  	$document_id = $request->getParameter('id');
  	$document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));

    $document->setAuthorId($this->getUser()->getId());
    $this->form = new DocumentForm($document);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    
    $document_id = $request->getParameter('id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));
    
    $document->setAuthorId($this->getUser()->getId());
    $this->form = new DocumentForm($document);
    $this->processForm($request, $this->form);
    $this->setTemplate('edit');    
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $document_id = $request->getParameter('id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));

    $user_id = $this->getUser()->getId();
    
    $creator_id = DocumentVersionTable::findCreatorId($document_id);   
    if ($creator_id != $user_id)
      $this->redirect('document_delete_error',$document );
    
    $document->setAuthorId($user_id);
    $document->setIsDeleted(1);    
    $document->save();
    
    $this->redirect('@homepage');
  }

  public function executeDeleteError(sfWebRequest $request)
  {
    $document_id = $request->getParameter('id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));
    
    $user_id = $this->getUser()->getId();
    $creator_id = DocumentVersionTable::findCreatorId($document_id);   
    if ($creator_id != $user_id)
      $this->errorMessage = "You are not the author of the document \"" . $document->getName() . "\". " . 
        "That is why You have no rights to delete it.";
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	$formNotValid = $form;
  	
  	$document = $form->getObject();
  	
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $document = $form->save();

      $this->redirect('@homepage');
    }
  }

   public function executeVersionFiles(sfWebRequest $request)
  {
      $version_id = $request->getParameter('version');
      $document_id = $request->getParameter('id');

      $this->files_id = DocumentVersionFilesTable::getInstance()->findAllExistingFilesOfDocument($document_id,$version_id);
      $this->document = DocumentTable::getInstance()->findOneBy('id',$document_id);
      $this->getUser()->addDocumentToHistory($document_id);

      $this->show_file=true;
      $user_id = $this->getUser()->getId();
      $creator_id = DocumentVersionTable::findCreatorId($document_id);
      if ($creator_id != $user_id)
        $this->show_file=false;
  }
}